﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PionAatuo : MonoBehaviour {
    public string[,] dataA;
    public Vector2[,] mapsA;
    public Transform mapuA2;
    public Transform mapdA2;
    Vector2 staerv2;
    Vector2 endv2;
    Vector3[] poin;
    // Use this for initialization
    void Start () {
        dataA = new string[200,200];
        mapsA = new Vector2[200,200];
        poin = new Vector3[]
        {
            new Vector3(0,0,0),
            new Vector3(1,0,0),
            new Vector3(1,0,0),
            new Vector3(1,0,1),
            new Vector3(1,0,1),
            new Vector3(0,0,1),
            new Vector3(0,0,1),
            new Vector3(0,0,0)
        };
        for (int x = 0; x < 200; x++)
        {
            for (int y = 0; y < 200; y++)
            {
                dataA[x, y] = "A00,"+x+","+y+",00";
                mapsA[x, y] = new Vector2(mapuA2.position.x + x, mapuA2.position.z + y);
            }
        }

    }



    LineRenderer _lineRenderer;
    string name;
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2000f))
            {
                staerv2 = new Vector2(hit.point.x - mapuA2.position.x, hit.point.z-mapuA2.position.z);
               
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2000f))
            {
                endv2= new Vector2(hit.point.x - mapuA2.position.x, hit.point.z - mapuA2.position.z);

                if (staerv2.x>endv2.x&&staerv2.y>endv2.y)
                {
                    for (int x = (int)endv2.x; x < (int)staerv2.x; x++)
                    {
                        for (int y = (int)endv2.y; y < (int)staerv2.y; y++)
                        {
                            _lineRenderer = new GameObject().AddComponent<LineRenderer>();
                            _lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
                            _lineRenderer.startColor = Color.red;
                            _lineRenderer.endColor = Color.red;
                            _lineRenderer.widthMultiplier = 0.5f;
                            _lineRenderer.startWidth = 1f;
                            _lineRenderer.useWorldSpace = false;
                            _lineRenderer.transform.parent = mapuA2;
                            _lineRenderer.transform.position = hit.point;
                            _lineRenderer.transform.localPosition = new Vector3(x, _lineRenderer.transform.localPosition.y, y);
                            _lineRenderer.positionCount = 8;
                            _lineRenderer.SetPositions(poin);
                        }
                    }
                }



                //for (int x = 0; x < Mathf.Abs((int)staerv2.x-(int)endv2.x); x++)
                //{
                //    for (int y = 0; y < Mathf.Abs((int)staerv2.y - (int)endv2.y); y++)
                //    {
                //        _lineRenderer = new GameObject().AddComponent<LineRenderer>();
                //        _lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
                //        _lineRenderer.startColor = Color.red;
                //        _lineRenderer.endColor = Color.red;
                //        _lineRenderer.widthMultiplier = 0.1f;
                //        _lineRenderer.startWidth = 0.01f;
                //        _lineRenderer.useWorldSpace = false;
                //        _lineRenderer.transform.parent = mapuA2;
                //        _lineRenderer.transform.localPosition = new Vector3(-(int)staerv2.x - 1, 10, (int)staerv2.y);
                //        _lineRenderer.positionCount = 8;
                //        _lineRenderer.SetPositions(poin);
                //    }
                //}
            }
        }
    }
    private void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 100, 30), name))
        {
            if (name== "出租")
            {
                dataA[(int)staerv2.x, (int)staerv2.y] = "A00出租";
                name = "收回";
            }
            else
            {
                dataA[(int)staerv2.x, (int)staerv2.y] = "A00," + (int)staerv2.x + "," + (int)staerv2.y + ",00";
                name = "出租";
            }
        }
       
    }
}
